package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.UserMapper;
import cn.tedu.egweibo2.pojo.ato.UserDTO;
import cn.tedu.egweibo2.pojo.entity.User;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Slf4j
@RequestMapping(value = "/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping(value = "reg")
    public int usersReg(@RequestBody UserDTO userDTO) {
        log.debug("userDTO = {}", userDTO);
        UserVO uservo = userMapper.select(userDTO.getUsername());
        if (uservo != null) {
            return 2;
        }
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return 1;
    }

}
